import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { config } from '../../shared/smartadmin.config';

declare var $: any;
declare var echarts: any;
@Component({
	selector: 'sa-replenish-order',
	templateUrl: './replenish-order.component.html',
	styleUrls: ['./replenish-order.component.css']
})
export class ReplenishOrderComponent implements OnInit {
	validatorOption = {
		excluded: [":disabled"],
		feedbackIcons: {
			valid: 'glyphicon glyphicon-ok',
			invalid: 'glyphicon glyphicon-remove',
			validating: 'glyphicon glyphicon-refresh'
		},
		fields: {
			number: {
				validators: {
					callback: {
						callback: function (value, validator, $field) {
							if (value) {
								let isValid = /(^[1-9]\d*$)/.test(value);
								if (isValid) {
									return true;
								} else {
									return {
										valid: false,
										message: '请输入正整数'
									}
								}
							} else {
								return {
									valid: false,
									message: '请输入订单数(套)'
								}
							}
						}.bind(this)
					}

				}
			},
			newmaterielNumber: {
				trigger: "change",
				validators: {
					notEmpty: {
						message: '请选择BOM编码'
					}
				}
			}
		}
	}
	validatorOptions = {
		excluded: [":disabled"],
		feedbackIcons: {
			valid: 'glyphicon glyphicon-ok',
			invalid: 'glyphicon glyphicon-remove',
			validating: 'glyphicon glyphicon-refresh'
		},
		fields: {
			projectName: {
				trigger: "change",
				validators: {
					notEmpty: {
						message: '请选择项目'
					}
				}
			},
			// type: {
			// 	validators: {
			// 		notEmpty: {
			// 			message: '请选择销售类型'
			// 		}
			// 	}
			// },
			linkMan: {
				validators: {
					notEmpty: {
						message: '请输入联系人'
					}
				}
			},
			linkPhone: {
				validators: {
					notEmpty: {
						message: '请输入联系电话'
					},
					integer: {
						message: '请输入数字电话号码'
					}
				}
			},
			address: {
				validators: {
					notEmpty: {
						message: '请输入邮寄地址'
					}
				}
			},
			pmName: {
				trigger: "change",
				validators: {
					notEmpty: {
						message: '请选择项目经理'
					}
				}
			},
			projectCompanyName: {
				validators: {
					notEmpty: {
						message: '请选择所属公司'
					}
				}
			}
		}
	}
	startTime = ''; //过滤条件-开始时间
	endTime = ''; //过滤条件-结束时间
	selectedCompanyId = ''; //过滤条件-公司ID
	selectedCompanyName = ''; //过滤条件-公司名称
	deviceId: string = '';
	carNumber: string = '';

	type: string = '';
	isFinish: string = '';
	tableData = []; //table数据
	totalCount: number = 0; //分页总条数
	curPage: number = 1; //分页当前页
	pageSize: number = 10; //内置的分页每页条数
	searchKey: string = ''; //搜索keyword
	formType: string = 'new';
	bomOrderFromSalesType: number = 1; //默认弹窗销售类型 为销售
	projectName: string = '';//项目名称
	projectId: string = '';//项目Id
	projectList: any = [];//项目List
	fileUp: string = '';
	fileToUpload: File = null; //上传文件回调参数
	constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute) {
		activatedRoute.queryParams.subscribe(queryParams => { });
	}
	// 公司选中回调
	companySelected(data) {
		this.selectedCompanyId = data.companyId;
		this.selectedCompanyName = data.companyName;
	}
	// 时间选中回调
	datePickerSelected(time) {
		this.startTime = time.startTime;
		this.endTime = time.endTime;
	}
	ngOnInit() {
		this.queryMaterial() //获取物料类型列表
		this.initAllProject().then(() => {
			this.selectProjectName();
			this.load();
			this.initPmSelect2();
		});
	}
	refresh() {
		this.load();
	}
	// 刷新
	table_search() {
		this.curPage = 1;
		this.load();
	}
	// 初始化加载list数据
	load() {
		let data = {
			pageSize: this.pageSize,
			nowPage: this.curPage,
			parameters: {
				companyId: this.selectedCompanyId,
				type: this.type,
				isFinish: this.isFinish,
				projectName: this.projectName,
				searchKey: common.stringTrim(this.searchKey),
				startTime: this.startTime,
				endTime: this.endTime,
			}
		}
		let reqdata = 'gridPager=' + JSON.stringify(data);
		this.jsonApiService.http("replenishmentOrder/replenishmentOrderForPage.html", reqdata).subscribe(data => {
			this.tableData = data.exhibitDatas;
			this.totalCount = data.recordCount;
		})
	}
	// 分页回调
	paginate(e) {
		this.curPage = e.page + 1;
		this.pageSize = e.rows;
		this.load();
	}
	// 擦除项目所选值
	del_project() {
		this.projectName = '';
		this.projectId = '';
		// 采用简单粗暴的方法清除;官方推荐的方法失效
		var str = $("<span class='select2-selection__placeholder'>请选择</span>");
		$('#projectSelect2').val("");
		$("#select2-projectSelect2-container").empty();
		$("#select2-projectSelect2-container").attr("title", "");
		$("#select2-projectSelect2-container").prepend(str);
		this.load();
	}
	// 擦除项目所选值
	delProjectNameFrom() {
		this.bomOrderFrom.projectName = '';
		this.bomOrderFrom.projectId = '';
		$("#projectName").val("").change();
		//采用简单粗暴的方法清除;官方推荐的方法失效
		var str = $("<span class='select2-selection__placeholder'>请选择</span>");
		$('#projectNameSelect2From').val("");
		$("#select2-projectNameSelect2From-container").empty();
		$("#select2-projectNameSelect2From-container").attr("title", "");
		$("#select2-projectNameSelect2From-container").prepend(str);
	}
	// 初始化select2 选项
	selectProjectName() {
		var that = this;
		System.import('script-loader!select2/dist/js/select2.min.js').then(() => {
			System.import('assets/js/select2-zh-CN.js').then(() => {
				$("#projectSelect2").select2({
					language: "zh-CN",
					placeholder: "请选择",
					allowClear: true,
					data: that.projectList,//为Select添加数据
					templateResult: that.formatRepo
				}).on("select2:select", function (e) {
					var data = e.params.data;
					that.projectName = data.text;
					that.projectId = data.id;
				});
				$("#projectNameSelect2From").select2({
					language: "zh-CN",
					placeholder: "请选择",
					allowClear: true,
					data: that.projectList,//为Select添加数据
					templateResult: that.formatRepo
				}).on("select2:select", function (e) {
					var data = e.params.data;
					that.bomOrderFrom.projectName = data.text;
					// that.bomOrderFrom.projectId = data.id;
					// that.bomOrderFrom.projectId = data.id;
					// that.bomOrderFrom.companyName = data.data.companyName;
					// that.bomOrderFrom.companyId = data.data.companyId;
					// that.bomOrderFrom.linkMan = data.data.linkMan;
					// that.bomOrderFrom.linkPhone =  data.data.linkPhone;
					// that.bomOrderFrom.address = data.data.address;
					$("#projectName").val(that.bomOrderFrom.projectName).change();
					$("#bomOrderFrom").data('bootstrapValidator').resetForm();
					that.initAllProjectName().then(() => {
						that.selectCompanyNameChange()
					})
				});

			})
		})
	}
	// select2 格式回调函数
	formatRepo(state) {
		if (!state.id) { return state.text };
		var $state = $('<span><i class="fa fa-user green"></i>&nbsp;&nbsp;' + state.text + '</span>');
		return $state;
	}
	// 拉取
	initAllProject() {
		var that = this;
		return new Promise(function (resolve, reject) {
			that.jsonApiService.http("project/projectList.html", null).subscribe(data => {
				if (data.success) {
					var projectList = data.data;
					var selectdata = [];
					for (var i = 0; i < projectList.length; i++) {
						var obj = {
							id: projectList[i].id,
							text: projectList[i].projectName,
							data: projectList[i],
						}
						selectdata[i] = obj;
					}
				}
				that.projectList = selectdata;
				resolve();
			})
		})
	}
	companyNameList: any = []; //该项目下所有的所属厂区
	// 拉取项目下面所属公司
	initAllProjectName() {
		var that = this;
		let reqdata = 'projectName=' + this.bomOrderFrom.projectName;
		return new Promise(function (resolve, reject) {
			that.jsonApiService.http("project/queryByProjectName.html", reqdata).subscribe(data => {
				if (data.success) {
					that.companyNameList = [];
					var companyNameList = data.data;
					var selectdata = [];
					for (var i = 0; i < companyNameList.length; i++) {
						var obj = {
							id: companyNameList[i].companyId,
							text: companyNameList[i].companyName,
							data: companyNameList[i],
						}
						selectdata[i] = obj;
					}
					that.companyNameList = selectdata;
				}
				resolve();
			})
		})

	}
	bomOrderFrom: any = {
		id: '',
		projectName: '',
		projectId: '',
		companyName: '',
		companyId: '',
		type: '',
		linkMan: '',
		linkPhone: '',
		address: '',
		pmName: '',
		pmId: ''
	}
	// 添加
	table_add(modal) {
		this.bomOrderFromSalesType = 1;
		this.formType = 'new';
		this.bomOrderFrom.type = 1;
		$("#bomOrderFrom").data('bootstrapValidator').resetForm();
		modal.show()
		// $('#bomOrderModal').modal('show');
	}
	// 编辑
	table_edit(row, modal) {
		
		// $('#bomOrderModal').modal('show');
		var that = this;
		this.formType = 'updata';
		this.bomOrderFrom.id = row.id;
		let reqdata = 'id=' + row.id;
		this.jsonApiService.http("replenishmentOrder/getReplenishmentOrderInfo.html", reqdata).subscribe(data => {
			if (data.success) {
				let result = data.data;
				this.bomOrderFrom.projectName = data.data.projectName;
				// $("#projectNameSelect2From").val(data.data.projectId).trigger('change');
				$("#pmNameSelect2From").val(data.data.pmId).trigger('change');
				// 因为返回的id在选择框中筛选过了
				var str = $("<span class='select2-selection__rendere'>" + data.data.projectName + "</span>");
				$('#projectNameSelect2From').val("");
				$("#select2-projectNameSelect2From-container").empty();
				$("#select2-projectNameSelect2From-container").attr("title", "");
				$("#select2-projectNameSelect2From-container").prepend(str);
				that.initAllProjectName().then(() => {
					for (let key in that.bomOrderFrom) {
						that.bomOrderFrom[key] = result[key];
						if (key == 'projectName') {
							$('#projectName').val(result[key]).change()
						}
						if (key == 'pmId') {
							$("#pmName").val(result[key]).change()
						}
					}
					that.bomOrderFromSalesType = result.type;
				});
				// this.initAllProjectName()
				modal.show()
			}
		})
	}
	// 关闭新增销售订单弹窗
	bomOrderClose(modal) {
		modal.hide();
		// $('#bomOrderModal').modal('hide');
		this.clearBomOrderPage();
	}
	clearBomOrderPage() {
		$("#bomOrderFrom").data('bootstrapValidator').resetForm();
		this.delProjectNameFrom();
		this.delpmNameForm();
		this.bomOrderFrom = {
			id: '',
			projectName: '',
			projectId: '',
			companyName: '',
			companyId: '',
			type: 1,
			linkMan: '',
			linkPhone: '',
			address: '',
			pmName: '',
			pmId: ''
		}
	}
	// 新增销售订单提交
	bomOrderSubmit(modal) {
		if (this.bomOrderFrom.type == "") {
			this.bomOrderFrom.type = 1
		}
		let obj = JSON.parse(JSON.stringify(this.bomOrderFrom))
		$('#bomOrderFrom').bootstrapValidator('validate'); //开启验证
		let isValid = $('#bomOrderFrom').data('bootstrapValidator').isValid();  //验证是否通过
		if (isValid) {
			let url;
			if (this.formType == 'new') {
				url = "replenishmentOrder/addReplenishmentOrder.html";
				for (var i = 0; i < this.companyNameList.length; i++) {
					if (this.companyNameList[i].id == this.bomOrderFrom.companyId) {
						this.bomOrderFrom.companyName = this.companyNameList[i].text
					}
				}
			} else if (this.formType == 'updata') {
				url = "replenishmentOrder/editReplenishmentOrder.html";
			}
			this.jsonApiService.http_obj(url, this.bomOrderFrom).subscribe(data => {
				this.jsonApiService.message(data.message);
				if (data.success) {
					let reqdata = {
						id: obj.projectId,
						projectName: obj.projectName,
						companyId: obj.companyId,
						companyName: obj.companyName,
						pmId: obj.pmId,
						pmName: obj.pmName,
						linkPhone: obj.linkPhone,
						linkMan: obj.linkMan,
						address: obj.address,
						isUpdate: 1, //编辑
						// enterMan: this.bomOrderFrom.enterMan,
					}
					this.jsonApiService.http_obj('project/updateProject.html', reqdata).subscribe(res => {
					})
					this.load();
					this.bomOrderClose(modal);
				}
			})
		}
	}
	// 删除
	table_del(row) {
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(() => {
			let reqdata = 'id=' + row.id;
			this.jsonApiService.http("replenishmentOrder/deleteById.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				if (data.success) {
					this.load();
				}
			})
		})
	}
	//选中销售类型
	clickSalesType(type) {
		this.bomOrderFrom.type = type;
		this.bomOrderFromSalesType = type
	}
	//所属公司select选中
	selectCompanyNameChange() {
		for (var i = 0; i < this.companyNameList.length; i++) {
			if (this.companyNameList[i].id == this.bomOrderFrom.companyId) {
				this.bomOrderFrom.linkMan = this.companyNameList[i].data.linkMan;
				this.bomOrderFrom.linkPhone = this.companyNameList[i].data.linkPhone;
				this.bomOrderFrom.address = this.companyNameList[i].data.address;
				this.bomOrderFrom.projectId = this.companyNameList[i].data.id;
				// 
				this.bomOrderFrom.companyId = this.companyNameList[i].data.companyId;
				this.bomOrderFrom.companyName = this.companyNameList[i].data.companyName;
				if (this.companyNameList[i].data.pmName && this.companyNameList[i].data.pmId) {
					this.bomOrderFrom.pmName = this.companyNameList[i].data.pmName;
					this.bomOrderFrom.pmId = this.companyNameList[i].data.pmId;
					$("#pmNameSelect2From").val(this.bomOrderFrom.pmId).trigger('change');
				} else {
					this.bomOrderFrom.pmName = "";
					this.bomOrderFrom.pmId = "";
					$("#pmNameSelect2From").val("").trigger('change');
				}
				$("#bomOrderFrom").data('bootstrapValidator').resetForm();
			}
		}
	}
	/**
		 * 补货单审核模块===========================================================================================================================================
		 */
	auditForm: any = {
		id: '',
		status: '1',
		remark: ''
	}
	cancelAudit(val) {
		val.hide();
	}
	auditSubmit(val) {
		if (!this.auditForm.status) {
			this.jsonApiService.message("请先选择审核状态");
			return
		}
		this.jsonApiService.http_obj("replenishmentOrder/checkReplenishmentOrder.html", this.auditForm).subscribe(data => {
			this.jsonApiService.message(data.message);
			this.load();
			val.hide();
		})
	}
	//审核状态改变
	auditStatusChange(val) {
		this.auditForm.status = val;
	}
	//安装记录审核
	table_check(row, val) {
		this.auditForm.id = row.id;
		this.auditForm.remark = '';
		this.auditForm.status = '1';
		val.show();
	}
	/**
	 * 订单导入报表模块===========================================================================================================================================
	 */

	// 显示导入的弹框
	showImportModal(modal) {
		modal.show();
	}
	// 关闭导入的弹框
	cancelUp(val) {
		val.hide();
		this.fileToUpload = null;
	}
	// 下载模板
	load_module() {
		var elemIF = document.createElement("iframe");
		elemIF.src = config.BaseURL + 'resources/template/bom-Template.xlsx';
		elemIF.style.display = "none";
		document.body.appendChild(elemIF);
	}
	// 清空input type=file输入框的值
	clearFile() {
		this.fileUp = '';
	}

	//文件上传change回调
	fileChange(files: FileList) {
		this.fileToUpload = files.item(0);
		const isEXCEL = this.fileToUpload.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' || this.fileToUpload.type === "application/vnd.ms-excel";
		if (!isEXCEL) {
			this.jsonApiService.message('上传文件只能是 excel格式!');
			this.fileToUpload = null;
			return;
		}
	}

	//导入submit
	submitData(val) {
		let postParam = new FormData();
		postParam.append('file', this.fileToUpload);
		if (!this.fileToUpload) {
			this.jsonApiService.message('请上传文件')
			return false;
		}
		this.jsonApiService.form_http("replenishmentOrder/uploadOrder.html", postParam).subscribe(data => {
			this.jsonApiService.message(data.message);
			if (data.success) {
				val.hide();
				this.fileToUpload = null;
				this.load();
			}
		})
	}

	/**
	 *   BOM清单明细模块===========================================================================================================================================
	 */

	bomOrderRow: any;
	BomTableData: any = [];
	// BOM明细打开事件
	projectBom(row, modal) {
		modal.show();
		this.bomOrderRow = row;
		this.bomCodePage.id = row.id;
		this.bomOrderCodeForm.orderId = row.id;
		this.getProjectBom(row.id);
		this.getAllBOMTypeList();
		this.getAllBOM().then(() => { this.bomNumberSelect2Config() });
	}
	// BOM明细清单
	getProjectBom(id) {
		this.jsonApiService.http("replenishmentOrder/getMaterielListById.html", 'id=' + id).subscribe(data => {
			if (data.success) {
				this.BomTableData = data.data;
			}
		})
	}
	// BOM明细弹窗关闭
	bomOrderCodeClose(modal) {
		modal.hide();
	}
	// BOM编码明细删除
	delBOMCode(row) {
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(() => {
			let reqdata = {
				id: this.bomOrderRow.id,
				materielNumber: row.materielNumber,
			}
			this.jsonApiService.http_obj("replenishmentOrder/delMaterielForOrder.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				if (data.success) {
					this.getProjectBom(this.bomOrderRow.id);
					this.load();
				}
			})
		})
	}
	/**
	 *  BOM编码明细编辑模块===========================================================================================================================================
	 */
	bomOrderCodeForm: any = {
		orderId: '',
		materielNumber: '',
		number: '',
		newmaterielNumber: '',
	}
	bomCodeEditRow: any;
	// BOM编码明细编辑弹窗打开
	editBOMCode(row) {
		$('#bomOrderCodeEditModal').modal('show');
		this.bomCodeEditRow = row;
		this.bomOrderCodeForm.number = row.num;
		this.bomOrderCodeForm.materielNumber = this.bomOrderCodeForm.newmaterielNumber = row.materielNumber;
		$('#bomNumberEditSelect2').val(row.materielNumber).trigger('change');
	}
	// BOM编码明细编辑弹窗关闭
	bomOrderCodeEditClose() {
		$('#bomOrderCodeEditModal').modal('hide');
		this.bomOrderCodeForm.number = '';
		this.delBomNumberEdit();
		$("#bomOrderCodeEditFrom").data('bootstrapValidator').resetForm();
	}
	//删除bom编辑值
	delBomNumberEdit() {
		this.bomOrderCodeForm.newmaterielNumber = '';
		$("#newmaterielNumber").val('').change();
		//采用简单粗暴的方法清除;官方推荐的方法失效
		var str = $("<span class='select2-selection__placeholder'>请选择</span>");
		$('#bomNumberEditSelect2').val("");
		$("#select2-bomNumberEditSelect2-container").empty();
		$("#select2-bomNumberEditSelect2-container").attr("title", "");
		$("#select2-bomNumberEditSelect2-container").prepend(str);
	}
	// BOM编码明细编辑提交
	bomOrderCodeEditSubmit() {
		$('#bomOrderCodeEditFrom').bootstrapValidator('validate'); //开启验证
		let isValid = $('#bomOrderCodeEditFrom').data('bootstrapValidator').isValid();  //验证是否通过
		if (isValid) {
			this.jsonApiService.http_obj("replenishmentOrder/editMaterielToReplenishmentOrder.html", this.bomOrderCodeForm).subscribe(data => {
				this.jsonApiService.message(data.message);
				if (data.success) {
					this.getProjectBom(this.bomOrderRow.id);
					this.load();
					$('#bomOrderCodeEditModal').modal('hide');
				}
			})
		}
	}
	/**
	 * 订单:BOM编码添加===========================================================================================================================================
	 */
	bomCodePage: any = {
		id: '',
		materielNumber: '',
		number: '',
	}
	allBomList: any = [];// 所有BOM数据
	bomfilterList: any = [];// 所有BOM-主机类型,传感器类型,线材类型数据
	bomFilterParam: any = {
		materielNumber: '', //物料编码
		materielType: '', //物料类型(1主机 2传感器 3线材 4天线 5基座 6显示器)
		productName: '', //产品名称
	}
	bomCodePageList: any = [];
	// 新增BOM
	addBOMCodeForOrder() {
		this.bomCodePage.materielNumber = '';
		this.bomCodePage.number = '';
		this.bomFilterParam = {
			materielNumber: '', //物料编码
			materielType: '', //物料类型(1主机 2传感器 3线材 4天线 5基座 6显示器)
			productName: '', //产品名称
		}
		var str = $("<span class='select2-selection__placeholder'>请选择</span>");
		$('#bomNumberSelect2').val("");
		$("#select2-bomNumberSelect2-container").empty();
		$("#select2-bomNumberSelect2-container").attr("title", "");
		$("#select2-bomNumberSelect2-container").prepend(str);
		this.bomCodePageList = this.allBomList;
		if (this.bomCodePageList.length > 0) {
			for (let item of this.bomCodePageList) {
				item.selected = false;
				item.count = 1;
			}
		}
		$('#bomCodePageModal').modal('show');
	}
	// BOM编码新增弹窗关闭
	bomCodePageClose() {
		$('#bomCodePageModal').modal('hide');
		this.bomCodePage.materielNumber = '';
		this.bomCodePage.number = '';
	}
	bomFilterParamChange() {
		this.getBOMByParam();
	}
	// 所有BOM-主机类型,传感器类型,线材类型数据
	getAllBOMTypeList() {
		this.jsonApiService.http("bomMaterialController/addBomInit.html", null).subscribe(data => {
			if (data.success) {
				this.bomfilterList = data.data;
			}
		})
	}
	// 根据过滤参数查询BOM
	getBOMByParam() {
		this.jsonApiService.http_obj("bomMaterialController/queryMaterielList.html", this.bomFilterParam).subscribe(data => {
			if (data.success) {
				let result = data.data;
				for (let item of result) {
					item['selected'] = false;
					item['count'] = 1;
				}
				this.bomCodePageList = result;
			}
		})
	}
	// 获取所有BOM列表
	getAllBOM() {
		let that = this;
		return new Promise(function (resolve, reject) {
			that.jsonApiService.http("bomMaterialController/queryMaterielList.html", null).subscribe(data => {
				if (data.success) {
					that.allBomList = [];
					let result = data.data;
					for (let item of result) {
						item['selected'] = false;
						item['count'] = 1;
						item['text'] = item.productName;
						item['id'] = item.materielNumber;
					}
					that.allBomList = result;
					that.bomCodePageList = result;
					resolve()
				}
			})
		})
	}
	// 擦除项目所选值
	delBomNumber() {
		this.bomFilterParam.materielNumber = '';
		this.getBOMByParam();
		//采用简单粗暴的方法清除;官方推荐的方法失效
		var str = $("<span class='select2-selection__placeholder'>请选择</span>");
		$('#bomNumberSelect2').val("");
		$("#select2-bomNumberSelect2-container").empty();
		$("#select2-bomNumberSelect2-container").attr("title", "");
		$("#select2-bomNumberSelect2-container").prepend(str);
	}
	//初始化select2 选项
	bomNumberSelect2Config() {
		var that = this;
		System.import('script-loader!select2/dist/js/select2.min.js').then(() => {
			System.import('assets/js/select2-zh-CN.js').then(() => {
				$("#bomNumberSelect2").select2({
					language: "zh-CN",
					placeholder: "请选择",
					allowClear: true,
					data: this.allBomList,//为Select添加数据
					//			        templateResult: this.formatRepo
				}).on("select2:select", function (e) {
					var data = e.params.data;
					that.bomFilterParam.materielNumber = data.id;
					that.getBOMByParam();
				});
				$("#bomNumberEditSelect2").select2({
					language: "zh-CN",
					placeholder: "请选择",
					allowClear: true,
					data: this.allBomList,//为Select添加数据
				}).on("select2:select", function (e) {
					var data = e.params.data;
					that.bomOrderCodeForm.newmaterielNumber = data.id;
					$("#newmaterielNumber").val(data.id).change();
				});
			})
		})
	}
	// 激活的BOM
	BOMItemSelect(lis) {
		for (let item of this.bomCodePageList) {
			if (item.materielNumber === lis.materielNumber) {
				item.selected = !lis.selected;
			} else {
				item.selected = false;
			}
		}
	}
	// BOM编码新增提交
	bomCodePagetSubmit() {
		for (let item of this.bomCodePageList) {
			if (item.selected) {
				this.bomCodePage.materielNumber = item.materielNumber;
				this.bomCodePage.number = item.count;
				break;
			}
		}
		this.jsonApiService.http_obj("replenishmentOrder/addMaterielToOrder.html", this.bomCodePage).subscribe(data => {
			this.jsonApiService.message(data.message);
			if (data.success) {
				this.getProjectBom(this.bomOrderRow.id);
				this.load();
				this.bomCodePageClose();
			}
		})
	}
	// 路由跳转
	jumpToPage(e, url) {
		$('#bomOrderCodeModal').modal('hide');
		$('#bomOrderModal').modal('hide');
		e.stopPropagation();
		this.router.navigate([url]);
		setTimeout(function () {
			$('#left-panel').find('.top-menu-invisible.open').find('.UL').css('display', 'none');
			$('#left-panel').find('.top-menu-invisible.open').removeClass('open');
			$('#left-panel').find('.top-menu-invisible.active').addClass('open');
			$('#left-panel').find('.top-menu-invisible.active').find('.UL').css('display', 'block');
		}, 300)
	}
	// 物料明细的表单数据
	bomDetailTableData: any = []
	// 物料明细的弹框
	showBomDetail(row, modal) {
		this.bomDetailTableData = []
		let obj = {
			salesId: this.bomOrderCodeForm.orderId,
			bomNumber: row.bomNumber
		}
		this.jsonApiService.http_obj("replenishmentOrder/getMaterialDetails.html", obj).subscribe(data => {
			if (data.success) {
				if (data.data.length == 0) {
					this.jsonApiService.message(data.message);
					return;
				}

				this.bomDetailTableData = data.data
				modal.show();

			} else {
				this.jsonApiService.message(data.message);

			}
		})

	}
	cancelbomDetailModal(modal) {
		modal.hide()
	}
	// 初始化项目经理的select2
	initPmSelect2() {
		let that = this;
		this.jsonApiService.http("user/getUserList.html", null).subscribe(data => {
			if (data.success) {
				var userList = data.data;
				var selectdata = [];
				for (var i = 0; i < userList.length; i++) {
					var obj = {
						id: userList[i].id,
						text: userList[i].name,
					}
					selectdata[i] = obj;
				}
				System.import('script-loader!select2/dist/js/select2.min.js').then(() => {
					System.import('assets/js/select2-zh-CN.js').then(() => {
						$("#pmNameSelect2From").select2({
							language: "zh-CN",
							placeholder: "请选择",
							allowClear: true,
							data: selectdata,//为Select添加数据
							templateResult: this.formatRepo
						}).on("select2:select", function (e) {
							var data = e.params.data;
							that.bomOrderFrom.pmId = data.id
							that.bomOrderFrom.pmName = data.text
							$("#pmName").val("pmId").change();
						});
					})
				})
			}
		})
	}
	// 删除pm
	delpmNameForm() {
		this.bomOrderFrom.pmName = '';
		this.bomOrderFrom.pmId = '';
		$("#pmName").val("").change();
		//采用简单粗暴的方法清除;官方推荐的方法失效
		var str = $("<span class='select2-selection__placeholder'>请选择</span>");
		$('#pmNameSelect2From').val("");
		$("#select2-pmNameSelect2From-container").empty();
		$("#select2-pmNameSelect2From-container").attr("title", "");
		$("#select2-pmNameSelect2From-container").prepend(str);
	}

	materialTypeList: any = [] //物料类型列表数据
	//请求物料类型的接口
	queryMaterial() {
		this.jsonApiService.http('materialType/list.html', '').subscribe(data => {
			if (data.success) {
				this.materialTypeList = data.data
			}
		})
	}
}